//index.js
//获取应用实例
var util = require('../../utils/util.js');
const app = getApp()
Page({
  data: {
    tabbarIndex: '',
    indicatorDots: true,
    city: '',
    city1: '',
    autoplay: true,
    interval: 5000,
    duration: 500,
    logo: false,
    // martop: '170',
    navindex: 0,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    buytype: false,
    hidden: true,
    scrollHeight: 0,
    pageNum: 1,
    car_length_1: [],
    car_type_1: [],
    buyindex: 0,
    xingindex: 0,
    chose_c: true,
    movies: [

    ],
    nav: [
      { title: '汽配商城', pic: '/images/nav_01.png', url: '../product/product' },
      { title: '我的车辆', pic: '/images/nav_02.png', url: '../options/options?ismy=1' },
      { title: '订单中心', pic: '/images/nav_03.png', url: '../waybill/waybill' },
      { title: '充值', pic: '/images/nav_04.png', url: '../recharge/recharge' },
      
    ],
    nav1: [
      { title: '货运大厅', pic: '/images/nav_01.png', url: '../index/index' },
      { title: '确认装货', pic: '/images/nav_02.png', url: '' },
      { title: '确认发货', pic: '/images/nav_03.png', url: '' },
      { title: '我的违约', pic: '/images/nav_04.png', url: '../default/default' },

    ],

    //
    freight: [

    ],
    freightnav: [
      { txt: '全部' },
      { txt: '常跑路线' },
    ],
    // alltype: [
    //   {
    //     car_length: [

    //     ],
    //     car_type: [

    //     ],
    //   }
    // ],

    currentPage:1,
    deliver_city: '',
    accept_city:'',
    car_length:'',
    totalPage:1,
    car_type:''
  },
  // pullUpLoad() {
  
  // },
  confirm_car_type() {
    // 车长车型的选择确定按钮
    // 暂无功能，仅为收起选择面板
    this.typebuy();
  },
  onReachBottom:function(){
    var currentpage = this.data.currentPage;
    var totalpage = this.data.totalPage;
    if (currentpage < totalpage) {
      this.setData({
        currentPage: currentpage + 1
      })
      this.initData();
    } else {
      console.log("已经是全部数据")
      // wx.showToast({
      //   'icon':'loading',
      //   title: '已经是全部数据了',
      // })
    }
  },
  // 选择地址
  chose_city: function (e) {
    var type = e.currentTarget.dataset.type;
    var that = this;
    wx.navigateTo({
      url: '../address/address?type=' + type,
    })
  },
  navbin(e) {
    
    var url = e.currentTarget.dataset.url;
    var title = e.currentTarget.dataset.title;
    if(title == "订单中心"){
      var user_type = wx.getStorageSync('userInfo').user_type;
        if (user_type == 3) {
          app.navTo('../shipper_waybill/waybill');
          
        } else {
          app.navTo('../waybill/waybill');
          
        }
    } else if (title == "确认装货") {
      var user_type = wx.getStorageSync('userInfo').user_type;
      if (user_type == 3) {
        app.navTo('../shipper_waybill/waybill?type=5');

      } else {
        app.navTo('../waybill/waybill?type=11');

      }
    }
    else if (title == "确认发货") {
      var user_type = wx.getStorageSync('userInfo').user_type;
      if (user_type == 3) {
        app.navTo('../shipper_waybill/waybill?type=5');

      } else {
        app.navTo('../waybill/waybill?type=11');

      }
    }
    else if (title == "确认收货") {
      var user_type = wx.getStorageSync('userInfo').user_type;
      if (user_type == 3) {
        app.navTo('../shipper_waybill/waybill?type=6');

      } else {
        app.navTo('../waybill/waybill?type=12');

      }
    }
    else{
      app.navTo(url)
      app.swtTa(url)
    }
  },
  checkCertification: function (id) {
    var that = this;
    var user_type = wx.getStorageSync('userInfo').user_type;
    util.commonRequest('getUserCertification', 'post', {}, function (res) {

      if (res.data.code == 200) {
        var stroeUser = wx.getStorageSync('userInfo');
        stroeUser.is_certification = res.data.data.is_certification;
        stroeUser.avatar = res.data.data.avatar;
        stroeUser.name = res.data.data.name;
        stroeUser.nick_name = res.data.data.nick_name;
        stroeUser.user_type = res.data.data.type;
        wx.setStorageSync('userInfo', stroeUser);
        if (res.data.data.is_certification == 0 || res.data.data.is_certification == 2) {
          wx.showToast({
            icon: 'loading',
            title: "请先认证",
          })
          if (user_type == 3){
            app.navTo('../shippercert/shippercert')
          } else{
            app.navTo('../drivercert/drivercert')
          }
          
        }else{
          app.navTo('../roaddeil/roaddeil?id=' + id)
        }
      } 
    });
  },
  typebuy: function () {
    this.setData({
      buytype: !this.data.buytype
    })
    var base = getApp().data.base_url;
    // this.getBanner(base)
  },
  //详情页
  start_roaddeil(e) {
    var id = e.currentTarget.dataset.id;
    this.checkCertification(id);

  },
  // 车长选择
  chose_baby(e) {
    var id = e.currentTarget.dataset.id;
    var value = e.currentTarget.dataset.value;
    this.setData({
      buyindex: id,
      car_length: value,
      currentPage: 1,
      freight: []
    })
    this.initData();
  },
  chose_buyx: function (e) {
    var id = e.currentTarget.dataset.id;
    var value = e.currentTarget.dataset.value;
    this.setData({
      xingindex: id,
      car_type: value,
      currentPage: 1,
      freight: []
    })
    this.initData();
   
  },
  click: function (e) {
    var id = e.currentTarget.dataset.id;
    if (id == 0) {
      this.setData({
        chose_c: true,
        chose_p: false,
        // martop: '170'
      })

    } else {
      this.setData({
        chose_p: true,
        chose_c: false,
        // martop: '80'
      })

    }
 
    console.log(id)
    if(id == 1){
      wx.navigateTo({
        url: '../oftenline/oftenline?ishome=1',
      })
    }else{
      this.setData({
        deliver_city: '',
        accept_city: '',
        currentPage: 1,
        freight: []
      })
      this.initData()
    }
    this.setData({
      navindex: id
    })

  },
  changecity: function (cityName, type) {

    if (type == 0) {
      // console.log("出发城市" + cityName)
      this.setData({
        city: cityName,
        currentPage: 1,
        deliver_city: cityName,
        freight:[]
    
      })
      // var base = getApp().data.base_url;
      this.initData()
    }
    else if (type == 1) {
      // console.log("送货城市" + cityName)
      this.setData({
        city1: cityName,
        currentPage: 1,
        accept_city: cityName,
        freight:[]
      })
      
      this.initData()
    }
  },
 
  getList(base) {

  },
  //登录
  sendRequest: function (res) {
    var that = this;
    wx.request({
      url: ' ',
      data: {

      },
      method: ' ',
      success: function (res) {

      }
    })
  },
 initData:function(){
   var that = this;
   var deliver_city = that.data.deliver_city
   var accept_city = that.data.accept_city
   if (deliver_city == '不限') { deliver_city ='';}
   if (accept_city == '不限') { accept_city = '';}
   var param = {
     currentPage: that.data.currentPage,
     deliver_city: deliver_city,
     accept_city: accept_city,
     car_length: that.data.car_length,
     car_type: that.data.car_type
   };
   wx.showLoading();
   util.commonRequest('tradeList', 'post', param, function (res) {
    wx.hideLoading();
     var data = res.data;
     if(data.code==200){
       var tempArr = data.data.data;
       var oriArr = that.data.freight;
       var finalyArr = oriArr.concat(tempArr);
       that.setData({
         freight: finalyArr,
         totalPage:data.data.last_page
       })
  
     }else{
       wx.showToast({
         icon:'loading',
         title: data.msg,
       })
     }
   });
 },
 initBanner:function(){
   var that = this;
   util.commonRequest('bannerList', 'post', {}, function (res) {
     if (res.data.code == 200) {
       that.setData({
         movies: res.data.data
       })
     } else {
       wx.showToast({
         icon: 'loading',
         title: res.data.msg,
       })
     }
   });
  }, 
  initCartLength:function(){
    var that = this;
    util.commonRequest('carLength', 'post', {}, function (res) {
      if(res.data.code==200){
        that.setData({
          car_length_1:res.data.data
        })
      }else{
        wx.showToast({
          icon:'loading',
          title: res.data.msg,
        })
      }
    });
  },
  initCartType: function () {
    var that = this;
    util.commonRequest('carType', 'post', {}, function (res) {
      if (res.data.code == 200) {
        that.setData({
          car_type_1: res.data.data
        })
      } else {
        wx.showToast({
          icon: 'loading',
          title: res.data.msg,
        })
      }
    });
  },
  onLoad: function (res) {

    wx.setStorageSync('cpline', '');

   this.initData();
   this.initCartLength();
   this.initCartType();
   this.initBanner();

    var base = getApp().data.base_url;
    //显示自定义的底部导航
    if (res.id == 1) {
      this.setData({
        tabbarIndex: res.id
      })
      getApp().editTabBar1();
    } else {
      getApp().editTabBar();
    }
 
    // 倒计时
    var freight = this.data.freight;
    

    if (!app.data.address.name) {
      var that = this;
      wx.getLocation({
        type: 'gcj02',
        altitude:true,
        success: function (res) {
          wx.setStorageSync('lat', res.latitude)
          wx.setStorageSync('lon', res.longitude)
          var param ={
            latng: res.latitude+","+res.longitude
          };
          util.commonRequest('cityByLatng', 'post',param, function (res) {});
        }
      })
    }
    this.setData({
      freight,
      city: app.data.address.name || "请选择",
      city1: app.data.address.name || "请选择",
    })
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    });
    // loadMore(that);
  },
  /**
  * 生命周期函数--监听页面显示
  */
  onShow: function () {
    var lineData = wx.getStorageSync('cpline');
    var lineS = lineData['route_origin'];
    var lineE = lineData['route_end'];
    if(lineS && lineE){
      this.setData({
        deliver_city: lineS,
        accept_city: lineE,
        currentPage:1,
        freight:[]
      })
      this.initData();
    }
  },
  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onPullDownRefresh() {
    console.log("下拉刷新")
    
    setTimeout(()=>{
      this.initData();
      this.initCartLength();
      this.initCartType();
      wx.stopPullDownRefresh()
      this.setData({
        city: app.data.address.name || "请选择",
        city1: app.data.address.name || "请选择",
      })
    },1000)
  }
})